package com.jingbit.ai.activity;

import android.Manifest;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.RequiresApi;

import com.baidu.idl.face.main.attribute.utils.AttributeConfigUtils;
import com.example.datalibrary.listener.SdkInitListener;
import com.jingbit.ai.AIApp;
import com.jingbit.ai.baidu.face.ActivitionActivity;
import com.jingbit.ai.baidu.face.FaceSDKManager;
import com.jingbit.ai.R;
import com.jingbit.ai.base.BaseActivity;
import com.tbruyelle.rxpermissions.RxPermissions;

import java.util.Timer;
import java.util.TimerTask;

import rx.functions.Action1;

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class StartActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);
        new RxPermissions(this).request(Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION,
                        Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.CHANGE_NETWORK_STATE,
                        Manifest.permission.CHANGE_WIFI_STATE, Manifest.permission.RECORD_AUDIO,
                        Manifest.permission.READ_CONTACTS, Manifest.permission.READ_PHONE_STATE,
                        Manifest.permission.CAMERA)//多个权限用","隔开
                .subscribe(new Action1<Boolean>() {
                    @Override
                    public void call(Boolean aBoolean) {
                        if (aBoolean) {
                            //当所有权限都允许之后，返回true
                            Log.i("permissions", "btn_more_sametime：" + aBoolean);
                            boolean isAttributeConfigExit = AttributeConfigUtils.isConfigExit(StartActivity.this);
                            boolean isAttributeInitConfig = AttributeConfigUtils.initConfig();

                            if (isAttributeInitConfig && isAttributeConfigExit) {
                                Toast.makeText(StartActivity.this, "初始配置加载成功", Toast.LENGTH_SHORT).show();
                            } else {
                                Toast.makeText(StartActivity.this, "初始配置失败,将重置文件内容为默认配置", Toast.LENGTH_SHORT).show();
                                AttributeConfigUtils.modityJson();
                            }
                            initLicense();
                        } else {
                            //只要有一个权限禁止，返回false，下一次申请只申请没通过申请的权限
                            Log.i("permissions", "btn_more_sametime：" + aBoolean);
                        }
                    }
                });


    }

    private void initLicense() {
        FaceSDKManager.getInstance().init(this, new SdkInitListener() {
            @Override
            public void initStart() {

            }

            public void initLicenseSuccess() {

                TimerTask task = new TimerTask() {

                    @Override
                    public void run() {
                        /**
                         *要执行的操作
                         */
                        startActivity(new Intent(StartActivity.this, FaceActivity.class));
                        finish();
                    }
                };
                Timer timer = new Timer();
                timer.schedule(task, 2000);
            }

            @Override
            public void initLicenseFail(int errorCode, String msg) {
                TimerTask task = new TimerTask() {
                    @Override
                    public void run() {
                        /**
                         *要执行的操作
                         */
                        startActivity(new Intent(StartActivity.this, ActivitionActivity.class));
                        finish();
                    }
                };
                Timer timer = new Timer();
                timer.schedule(task, 2000);
            }

            @Override
            public void initModelSuccess() {
            }

            @Override
            public void initModelFail(int errorCode, String msg) {

            }
        });
    }
}
